#include "grammar_parser.hpp"
#include "cpp-gen/ANTLRv4Lexer.h"
#include "cpp-gen/ANTLRv4Parser.h"
#include "cpp-gen/ANTLRv4ParserBaseListener.h"
#include "cpp-gen/ANTLRv4ParserBaseListener.h"

struct GrammarParser::GrammarParserImpl
{
    std::shared_ptr<antlr4::ANTLRInputStream> input;
    std::shared_ptr<ANTLRv4Lexer> lexer;
    std::shared_ptr<antlr4::CommonTokenStream> tokens;
    std::shared_ptr<ANTLRv4Parser> parser;
};

GrammarParser::GrammarParser()
{
    impl_.reset(new GrammarParserImpl);
}

GrammarParser::~GrammarParser()
{
}

std::shared_ptr<RuleCollection> GrammarParser::ruleElementCollections()
{
    return std::make_shared<RuleCollection>(impl_->parser->ruleCollection());
}

std::shared_ptr<RuleCollection> GrammarParser::invisibleRuleCollections()
{
    return std::make_shared<RuleCollection>(impl_->parser->invisibleRules());
}

void GrammarParser::init(const std::string &text)
{

    impl_->input.reset(new antlr4::ANTLRInputStream(text));
    impl_->lexer.reset(new ANTLRv4Lexer(impl_->input.get()));
    impl_->tokens.reset(new antlr4::CommonTokenStream(impl_->lexer.get()));
    impl_->tokens->fill();
    impl_->parser.reset(new ANTLRv4Parser(impl_->tokens.get()));
    impl_->parser->grammarSpec();
}
